$().ready(function(){
	$("#province_id").change(function(){ loadArea('city_id',$(this).val(),0); });
	$("#city_id").change(function(){ loadArea('district_id',$(this).val(),0); });	
});

function loadArea(obj_name,id,selected) {
	$.post(ajax_load_area_url,{'id':id},function(json){
			$obj = $("#"+obj_name);
			$obj.empty();
			$.each(json, function(i,v){
				if (v.id==selected) {
					sel = "selected";
				} else {
					sel = "";
				}
				$obj.append('<option value="'+v.id+'" '+sel+'>'+v.name+'</option>');
			});
		if(obj_name=='city_id' && selected==0){
			$.post(ajax_load_area_url,{'id':json[0].id},function(json){
				$obj2 = $("#district_id");
				$obj2.empty();
				$.each(json, function(i,v){
					if (v.id==selected) {
						sel = "selected";
					} else {
						sel = "";
					}
					$obj2.append('<option value="'+v.id+'" '+sel+'>'+v.name+'</option>');
				});

			},"json");
		}

		},"json");
}

function show_login() {
	tb_remove();	
	tb_show('登录', url_user_login + '&height=250&width=410&modal=false&TB_iframe', false);	
}

function show_register() {
	tb_remove();
	tb_show('注册', url_user_register +'&height=460&width=410&modal=false&TB_iframe', false);	
}

function show_activity_correct() {
	tb_remove();
	tb_show('活动报名成功', url_activity_correct +'&height=55&width=340&modal=false&TB_iframe', false);	
}

function show_activity_errorfyi() {
	tb_remove();
	tb_show('活动报名重复', url_activity_errorfyi +'&height=55&width=340&modal=false&TB_iframe', false);	
}

function show_activity_errorfer() {
	tb_remove();
	tb_show('活动报名失败', url_activity_errorfer +'&height=55&width=340&modal=false&TB_iframe', false);	
}

function show_activity_addact(id)
{
	tb_remove();
	tb_show('用户报名', url_activity_addact +'&id='+id+'&height=200&width=340&modal=false&TB_iframe', false);	
}

function show_socialpost_uspost(id) {
	tb_remove();
	tb_show('发布圈子信息', url_socialpost_uspost +'&id='+id+'&height=400&width=600&modal=false&TB_iframe', false);	
}

function show_useredit() {
	tb_remove();
	tb_show('修改会员资料', url_user_useredit +'&height=600&width=410&modal=false&TB_iframe', false);	
}

function show_socialgroup_gs() {
	tb_remove();
	tb_show('创建圈子', url_socialgroup_gs +'&height=260&width=410&modal=false&TB_iframe', false);	
}



/* ************************************************************** */
var tb_pathToImage = "Public/front/images/loadingAnimation.gif"; /*thickbox*/
$(document).ready(function() {
	tb_init("a.thickbox, area.thickbox, input.thickbox");
	imgLoader = new Image();
	imgLoader.src = tb_pathToImage
});

function tb_init(a) {
	$(a).click(function() {
		var c = this.title || this.name || null;
		var b = this.href || this.alt;
		var d = this.rel || false;
		tb_show(c, b, d);
		this.blur();
		return false
	})
}
function tb_show(j, b, h) {
	try {
		if (typeof document.body.style.maxHeight === "undefined") {
			$("body", "html").css({
				height: "100%",
				width: "100%"
			});
			$("html").css("overflow", "hidden");
			if (document.getElementById("TB_HideSelect") === null) {
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove)
			}
		} else {
			if (document.getElementById("TB_overlay") === null) {
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove)
			}
		}
		if (tb_detectMacXFF()) {
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack")
		} else {
			$("#TB_overlay").addClass("TB_overlayBG")
		}
		if (j === null) {
			j = ""
		}
		$("body").append("<div id='TB_load'><img src='" + imgLoader.src + "' /></div>");
		$("#TB_load").show();
		var c;
		if (b.indexOf("?") !== -1) {
			c = b.substr(0, b.indexOf("?"))
		} else {
			c = b
		}
		var f = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
		var k = c.toLowerCase().match(f);
		if (k == ".jpg" || k == ".jpeg" || k == ".png" || k == ".gif" || k == ".bmp") {
			TB_PrevCaption = "";
			TB_PrevURL = "";
			TB_PrevHTML = "";
			TB_NextCaption = "";
			TB_NextURL = "";
			TB_NextHTML = "";
			TB_imageCount = "";
			TB_FoundURL = false;
			if (h) {
				TB_TempArray = $("a[@rel=" + h + "]").get();
				for (TB_Counter = 0;
				((TB_Counter < TB_TempArray.length) && (TB_NextHTML === "")); TB_Counter++) {
					var g = TB_TempArray[TB_Counter].href.toLowerCase().match(f);
					if (!(TB_TempArray[TB_Counter].href == b)) {
						if (TB_FoundURL) {
							TB_NextCaption = TB_TempArray[TB_Counter].title;
							TB_NextURL = TB_TempArray[TB_Counter].href;
							TB_NextHTML = "<span id='TB_next'>&nbsp;&nbsp;<a href='#'>Next &gt;</a></span>"
						} else {
							TB_PrevCaption = TB_TempArray[TB_Counter].title;
							TB_PrevURL = TB_TempArray[TB_Counter].href;
							TB_PrevHTML = "<span id='TB_prev'>&nbsp;&nbsp;<a href='#'>&lt; Prev</a></span>"
						}
					} else {
						TB_FoundURL = true;
						TB_imageCount = "Image " + (TB_Counter + 1) + " of " + (TB_TempArray.length)
					}
				}
			}
			imgPreloader = new Image();
			imgPreloader.onload = function() {
				imgPreloader.onload = null;
				var n = tb_getPageSize();
				var l = n[0] - 150;
				var q = n[1] - 150;
				var m = imgPreloader.width;
				var e = imgPreloader.height;
				if (m > l) {
					e = e * (l / m);
					m = l;
					if (e > q) {
						m = m * (q / e);
						e = q
					}
				} else {
					if (e > q) {
						m = m * (q / e);
						e = q;
						if (m > l) {
							e = e * (l / m);
							m = l
						}
					}
				}
				TB_WIDTH = m + 30;
				TB_HEIGHT = e + 60;
				$("#TB_window").append("<a href='' id='TB_ImageOff' title='Close'><img id='TB_Image' src='" + b + "' width='" + m + "' height='" + e + "' alt='" + j + "'/></a><div id='TB_caption'>" + j + "<div id='TB_secondLine'>" + TB_imageCount + TB_PrevHTML + TB_NextHTML + "</div></div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a> or Esc Key</div>");
				$("#TB_closeWindowButton").click(tb_remove);
				if (!(TB_PrevHTML === "")) {
					function p() {
						if ($(document).unbind("click", p)) {
							$(document).unbind("click", p)
						}
						$("#TB_window").remove();
						$("body").append("<div id='TB_window'></div>");
						tb_show(TB_PrevCaption, TB_PrevURL, h);
						return false
					}
					$("#TB_prev").click(p)
				}
				if (!(TB_NextHTML === "")) {
					function o() {
						$("#TB_window").remove();
						$("body").append("<div id='TB_window'></div>");
						tb_show(TB_NextCaption, TB_NextURL, h);
						return false
					}
					$("#TB_next").click(o)
				}
				document.onkeydown = function(r) {
					if (r == null) {
						keycode = event.keyCode
					} else {
						keycode = r.which
					}
					if (keycode == 27) {
						tb_remove()
					} else {
						if (keycode == 190) {
							if (!(TB_NextHTML == "")) {
								document.onkeydown = "";
								o()
							}
						} else {
							if (keycode == 188) {
								if (!(TB_PrevHTML == "")) {
									document.onkeydown = "";
									p()
								}
							}
						}
					}
				};
				tb_position();
				$("#TB_load").remove();
				$("#TB_ImageOff").click(tb_remove);
				$("#TB_window").css({
					display: "block"
				})
			};
			imgPreloader.src = b
		} else {
			var a = b.replace(/^[^\?]+\??/, "");
			var d = tb_parseQuery(a);
			TB_WIDTH = (d.width * 1) + 30 || 630;
			TB_HEIGHT = (d.height * 1) + 40 || 440;
			ajaxContentW = TB_WIDTH - 30;
			ajaxContentH = TB_HEIGHT - 45;
			if (b.indexOf("TB_iframe") != -1) {
				urlNoQuery = b.split("TB_");
				$("#TB_iframeContent").remove();
				if (d.modal != "true") {
					$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + j + "</div><div id='TB_closeAjaxWindow'><a href='javascript:;' id='TB_closeWindowButton' title='关闭'>关闭</a></div></div><iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "' id='TB_iframeContent' name='TB_iframeContent" + Math.round(Math.random() * 1000) + "' onload='tb_showIframe()' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;' > </iframe>")
				} else {
					$("#TB_overlay").unbind();
					$("#TB_window").append("<iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "' id='TB_iframeContent' name='TB_iframeContent" + Math.round(Math.random() * 1000) + "' onload='tb_showIframe()' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;'> </iframe>")
				}
			} else {
				if ($("#TB_window").css("display") != "block") {
					if (d.modal != "true") {
						$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + j + "</div><div id='TB_closeAjaxWindow'><a href='javascript:;' id='TB_closeWindowButton'>关闭</a></div></div><div id='TB_ajaxContent' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px'></div>")
					} else {
						$("#TB_overlay").unbind();
						$("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px;'></div>")
					}
				} else {
					$("#TB_ajaxContent")[0].style.width = ajaxContentW + "px";
					$("#TB_ajaxContent")[0].style.height = ajaxContentH + "px";
					$("#TB_ajaxContent")[0].scrollTop = 0;
					$("#TB_ajaxWindowTitle").html(j)
				}
			}
			$("#TB_closeWindowButton").click(tb_remove);
			if (b.indexOf("TB_inline") != -1) {
				$("#TB_ajaxContent").append($("#" + d.inlineId).children());
				$("#TB_window").unload(function() {
					$("#" + d.inlineId).append($("#TB_ajaxContent").children())
				});
				tb_position();
				$("#TB_load").remove();
				$("#TB_window").css({
					display: "block"
				})
			} else {
				if (b.indexOf("TB_iframe") != -1) {
					tb_position();
					if ($.browser.safari) {
						$("#TB_load").remove();
						$("#TB_window").css({
							display: "block"
						})
					}
				} else {
					$("#TB_ajaxContent").load(b += "&random=" + (new Date().getTime()), function() {
						tb_position();
						$("#TB_load").remove();
						tb_init("#TB_ajaxContent a.thickbox");
						$("#TB_window").css({
							display: "block"
						})
					})
				}
			}
		}
		if (!d.modal) {
			document.onkeyup = function(l) {
				if (l == null) {
					keycode = event.keyCode
				} else {
					keycode = l.which
				}
				if (keycode == 27) {
					tb_remove()
				}
			}
		}
	} catch (i) {}
}
function tb_showIframe() {
	$("#TB_load").remove();
	$("#TB_window").css({
		display: "block"
	})
}
function tb_remove() {
	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut(0, function() {
		$("#TB_window,#TB_overlay,#TB_HideSelect").trigger("unload").unbind().remove()
	});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {
		$("body", "html").css({
			height: "auto",
			width: "auto"
		});
		$("html").css("overflow", "")
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false
}
function tb_position() {
	$("#TB_window").css({
		marginLeft: "-" + parseInt((TB_WIDTH / 2), 10) + "px",
		width: TB_WIDTH + "px"
	});
	if (!(jQuery.browser.msie && jQuery.browser.version < 7)) {
		$("#TB_window").css({
			marginTop: "-" + parseInt((TB_HEIGHT / 2), 10) + "px"
		})
	}
}
function tb_parseQuery(d) {
	var e = {};
	if (!d) {
		return e
	}
	var a = d.split(/[;&]/);
	for (var c = 0; c < a.length; c++) {
		var g = a[c].split("=");
		if (!g || g.length != 2) {
			continue
		}
		var b = unescape(g[0]);
		var f = unescape(g[1]);
		f = f.replace(/\+/g, " ");
		e[b] = f
	}
	return e
}
function tb_getPageSize() {
	var c = document.documentElement;
	var a = window.innerWidth || self.innerWidth || (c && c.clientWidth) || document.body.clientWidth;
	var b = window.innerHeight || self.innerHeight || (c && c.clientHeight) || document.body.clientHeight;
	arrayPageSize = [a, b];
	return arrayPageSize
}
function tb_detectMacXFF() {
	var a = navigator.userAgent.toLowerCase();
	if (a.indexOf("mac") != -1 && a.indexOf("firefox") != -1) {
		return true
	}
};


function addBookmark(){
    var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != -1 ? 'Command/Cmd': 'CTRL';
    try{
        if (document.all) { //IE类浏览器
            try {
                window.external.toString(); //360浏览器不支持window.external，无法收藏
                window.alert("国内开发的360浏览器等不支持主动加入收藏。\n您可以尝试通过浏览器菜单栏 或快捷键 ctrl+D 试试。");
            }
            catch (e){
                try{
                    window.external.addFavorite(window.location, document.title);
                }
                catch (e){
                    window.external.addToFavoritesBar(window.location, document.title);  //IE8
                }
            }
        }
        else if (window.sidebar) { //firfox等浏览器
            window.sidebar.addPanel(document.title, window.location, "");
        }
        else {
            alert('您可以尝试通过快捷键' + ctrl + ' + D 加入到收藏夹~');
        }
    }
    catch (e){
        window.alert("因为IE浏览器存在bug，添加收藏失败！\n解决办法：在注册表中查找\n HKEY_CLASSES_ROOT\\TypeLib\\{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}\\1.1\\0\\win32 \n将 C:\\WINDOWS\\system32\\shdocvw.dll 改为 C:\\WINDOWS\\system32\\ieframe.dll ");
    }
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9-_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
        output += match[1];
      x += match[1].length;
    } else {
      if (clearString.substr(x, 1) == ' ') {
        //原文在此用 clearString[x] == ' ' 做判断, 但ie不支持把字符串当作数组来访问, 
        //修改后两种浏览器都可兼容 
        output += '+';
      }
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}